#include "main.h"
extern struct Time time;


void Calculate_Weekday(struct Time *time)
{
    unsigned char i=0;
    if (time->Month == 1 || time->Month == 2)
    {
        time->Weekday = (time->Day + ((13 * (time->Month + 13)) / 5) + (time->Year - 1) + ((time->Year - 1) / 4) - (2 * ((time->Year - 1) / 100))) % 7;
    }
    else
    {
        time->Weekday = (time->Day + ((13 * (time->Month + 1)) / 5) + time->Year + (time->Year / 4) - (2 * (time->Year / 100))) % 7;
    }

    // 调整，使 Weekday 在 1~7 范围内
    time->Weekday = (time->Weekday + 5) % 7 + 1; // 使结果为 1~7

    // 如果 Weekday 为 0，设为 7
    if (time->Weekday == 0) {
        time->Weekday = 7;
    }
}

void main()
{
    unsigned char Key_Main=0;
    unsigned char Data = 0;
    LCD_Init();
    DS1302_Init();
    time.Year=00;
    time.Month=1;
    time.Day=1;
    time.Hour=0;
    time.Minute=0;
    time.Second=0;
    Calculate_Weekday(&time);
    DS1302_SetTime();
    LCD_ShowString(1,1,"    /  /  /");
    LCD_ShowNum(1,1,20,2);
    LCD_ShowString(2,1,"  :  :  ");
    while (1)
    {
        Key_Main=key();
        if(Key_Main==1)
        {
            LCD_ShowNum(2,10,Key_Main,2);
            Change_Time(&time);
        }
        DS1302_Showtime();
        LCD_ShowNum(1,3,time.Year,2);
        LCD_ShowNum(1,6,time.Month,2);
        LCD_ShowNum(1,9,time.Day,2);
        LCD_ShowNum(2,1,time.Hour,2);
        LCD_ShowNum(2,4,time.Minute,2);
        LCD_ShowNum(2,7,time.Second,2);
        LCD_ShowNum(1,12,time.Weekday,2);
    }
}

